/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package metro.jme.lottery.persist;

import java.util.Vector;
import javax.microedition.rms.InvalidRecordIDException;
import metro.jme.lottery.util.FormatChecker;

/**
 * This class handles the storage of the number set, that the user will input 
 * and save.
 * @author pierre
 */
public class SetBean {

    // Instance members
    private int id;
    private String value;
    // Class members
    private static Vector beanList = new Vector();

    // Default constructor
    public SetBean() {
    }

	/**
	 * 
	 * @param value 
	 */
    public SetBean(String value) {
        this.value = value;
    }

	/**
	 * 
	 * @param id
	 * @return 
	 */
    public static SetBean getBean(int id) {
        byte[] data = RMSUnit.getSet(id);
        SetBean s = null;
        if (data != null) {
            s = new SetBean(new String(data));
            s.setId(id);
        }
        return s;
    }

	/**
	 * 
	 * @return 
	 */
    public static SetBean[] getBeans() {
        System.out.println("getBeans");
        SetBean[] list = new SetBean[beanList.size()];
        SetBean setBean;
        for (int i = 0; i < beanList.size(); i++) {
            setBean = (SetBean) beanList.elementAt(i);
            list[i] = setBean;
        }
        return list;
    }

	/**
	 * 
	 */
    public void delete() {
        try {
            System.out.println("Deleting id:" + this.id);
            RMSUnit.delete(Persist.DBNAME_SETS, this.id);
        } catch (InvalidRecordIDException e) {
            System.err.println("Impossible to delete set row" + this.id);
        }

        System.out.println("Previous check :");
        for (int i = 0; i < beanList.size(); i++) {
            System.out.println("id :" + ((SetBean) beanList.elementAt(i)).getId());
        }

        beanList.removeElement(this);

        System.out.println("After check :");
        for (int i = 0; i < beanList.size(); i++) {
            System.out.println("id :" + ((SetBean) beanList.elementAt(i)).getId());
        }
    }

    /**
     * 
     */
    public static void deleteAll() {
        SetBean[] setBean = new SetBean[beanList.size()];
        setBean = SetBean.getBeans();
        System.out.println("Delete All :");
        System.out.println("State");
        System.out.println("beanList = " + toStringS());
        System.out.println("OK");

        for (int i = 0; i < setBean.length; i++) {
            System.out.println("i = " + i);
            System.out.println("setBean = " + setBean);
            setBean[i].delete();
            //beanList.removeElement(setBean);
        }
        // purge beanlist
        //beanList.removeAllElements();
    }

	/**
	 * 
	 */
    public void store() throws Exception {
        boolean valid = FormatChecker.checkUniqueness(value);
        if (!valid) {
            System.out.println("Non unique");
			throw new Exception("Error storing set");
        } else {
            System.out.println("Unique");
            this.id = RMSUnit.store(value.getBytes());
            beanList.addElement(this);
        }
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

	
    public static String toStringS() {
        String str = "";
        for (int i = 0; i < beanList.size(); i++) {
            str += "id : " + ((SetBean) beanList.elementAt(i)).getId() + "\n";
            str += "value : " + ((SetBean) beanList.elementAt(i)).getValue() + "\n\n";
        }
        return str;
    }

    public String toString() {
        String str = "";

        str += "id : " + this.getId() + "\n";
        str += "value : " + this.getValue() + "\n\n";

        return str;
    }
}
